﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.Contract;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using CommonsLib.Settings;
using CommonsLib.Utility;
using System.Data;

using System.Security.Cryptography;
using MaxLeaf.Platform.Functions;
using MaxLeaf.Platform.Commons;
using MaxLeaf.Platform.DALImplementation;




namespace MaxLeaf.Platform.BLLImplementation
{
    public class ImInvLocBLL : BaseBLL
    {


        public override string FinterIDFields
        {
            get
            {
                return string.Format("{0},{1}", "FInterID", (int)FinterIDType.ImInvLoc);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="intCompanyID"></param> 
        /// <param name="strItemFinterID"></param>
        /// <param name="strFromUnitID"></param> 要转到的单位ID 
        /// <param name="blnOnOrder"></param> 是否要减在途数量
        /// <param name="blnMin"></param>  是否要减安全库存
        /// <returns></returns>
        public static double GetAvail(int intCompanyID, int intItemFinterID, int intFromUnitID, bool blnOnOrder, bool blnMin)
        {
            using(DBAccess dbInvCon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPInv))
                ,dbBaseCon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase))
                )
            {
            string strSQL = "select measure_coefficient from goods_measure_unit where measure_unit_id='" + intFromUnitID + "'";
            double dblRate = CvtFunc.ToDouble(dbBaseCon.GetValue(strSQL));
            if (dblRate == 0) dblRate = 1;
            strSQL = "select * from iminvloc where itemid=@strItemFinterID and companyid=@companyid";
            using (DbDataReader drRead = dbInvCon.GetDataFirstRow(strSQL, new SqlParameter[] { new SqlParameter( "@strItemFinterID", intItemFinterID.ToString() ) 
                                                                                        ,new SqlParameter( "@companyid", intCompanyID) }))
            {
                if (drRead.Read())
                {
                    double dblReturn = 0;
                    double dblOnHand = CvtFunc.ToDouble(drRead["OnHand"]);
                    double dblOnOrder = CvtFunc.ToDouble(drRead["OnOrder"]);
                    double dblMin = CvtFunc.ToDouble(drRead["Min"]);
                    double dblMax = CvtFunc.ToDouble(drRead["Max"]);

                    dblReturn = dblOnHand;
                    if (blnOnOrder)
                    {
                        dblReturn = dblReturn + dblOnOrder;
                    }
                    if (blnMin)
                    {
                        dblReturn = dblReturn - dblMin;
                    }
                    return dblReturn / dblRate;
                }
            }
            return 0;
            }
        }

        public static double GetWtAvgCost(int intCompanyID, int intItemFInterID, int intFromUnitID)
        {
            double dblReturn = 0;
            using (DBAccess dbBaseCon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase))
            , dbInvCon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPInv))
            )
            {
                string strSQL = "select measure_coefficient from goods_measure_unit where measure_unit_id='" + intFromUnitID + "'";
                double dblRate = CvtFunc.ToDouble(dbBaseCon.GetValue(strSQL));
                if (dblRate == 0) dblRate = 1;


                strSQL = "select wtavgcost from iminvloc where companyid=@CompanyID and ItemID=@ItemID";
                dblReturn = CvtFunc.ToDouble(dbInvCon.GetValue(strSQL, new SqlParameter[]{ new SqlParameter ( "@CompanyID",intCompanyID.ToString ())
                                                                         , new SqlParameter ( "@ItemID", intItemFInterID.ToString ())}));
                dblReturn = dblReturn * dblRate;
                return dblReturn;
            }
        }

        public static bool UpdateOnHand(int intCompanyID, int intItemFInterID, double dblQty, double dblCost, int intUnitID)
        {
            double dblReturn = 0;
            using (DBAccess dbBaseCon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase))
                , dbInvCon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPInv))
            )
            {
                string strSQL = "select measure_coefficient from goods_measure_unit where measure_unit_id='" + intUnitID + "'";
                double dblRate = CvtFunc.ToDouble(dbBaseCon.GetValue(strSQL));
                if (dblRate == 0) dblRate = 1;

                double dblOrigOnHand = 0;
                strSQL = "Select onhand,wtavgcost from iminvloc where companyid=@CompanyID and ItemID=@ItemID";
                using (DbDataReader drInvLoc = dbInvCon.GetDataReader(strSQL, new SqlParameter[] { new SqlParameter ( "@CompanyID",intCompanyID.ToString ())
                                                                         , new SqlParameter ( "@ItemID", intItemFInterID.ToString ())}))
                {
                    if (drInvLoc.Read())
                    {
                        dblOrigOnHand = CvtFunc.ToDouble(drInvLoc["OnHand"]);
                    }
                    else
                    {
                        drInvLoc.Close();
                        ImInvLocDataModel imLoc = new ImInvLocDataModel();
                        imLoc.CompanyID = intCompanyID;
                        imLoc.ItemID = intItemFInterID;
                        imLoc.StockUnitID = CvtFunc.ToInt(dbBaseCon.GetValue("select StockUnitID from iminvmaster where FInterID='" + intItemFInterID + "'"));
                        new ImInvLocBLL().Add(imLoc);
                    }
                }
                dblReturn = dblReturn * dblRate;


                double dblInQty = dblQty * dblRate;

                double dblTotalQty = dblOrigOnHand + dblInQty;

                strSQL = @"update iminvloc set OnHand=OnHand+@InQtyInStockUnit from iminvloc 
                            where companyid=@CompanyID and ItemID=@ItemID";
                dbInvCon.ExecuteSql(strSQL, new SqlParameter[]{new SqlParameter ( "@InQtyInStockUnit",dblInQty.ToString ())                                                                        
                                                                         , new SqlParameter ( "@CompanyID",intCompanyID.ToString ())
                                                                         , new SqlParameter ( "@ItemID", intItemFInterID.ToString ())});

                return true;
            }
        }

        public static bool UpdateWtAvgCostAndOnHand(int intCompanyID, int intItemFInterID, double dblQty, double dblCost, int intUnitID)
        {
            double dblReturn = 0;
            using (DBAccess dbBaseCon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase))
            , dbInvCon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPInv))
            )
            {
                string strSQL = "select measure_coefficient from goods_measure_unit where measure_unit_id='" + intUnitID + "'";
                double dblRate = CvtFunc.ToDouble(dbBaseCon.GetValue(strSQL));
                if (dblRate == 0) dblRate = 1;

                double dblOrigTotalCost = 0;
                double dblOrigOnHand = 0;
                double dblOrigWtAvgCost = 0;

                strSQL = "Select onhand,wtavgcost from iminvloc where companyid=@CompanyID and ItemID=@ItemID";
                using (DbDataReader drInvLoc = dbInvCon.GetDataReader(strSQL, new SqlParameter[] { new SqlParameter ( "@CompanyID",intCompanyID.ToString ())
                                                                         , new SqlParameter ( "@ItemID", intItemFInterID.ToString ())}))
                {
                    if (drInvLoc.Read())
                    {
                        dblOrigTotalCost = CvtFunc.ToDouble(drInvLoc["OnHand"]) * CvtFunc.ToDouble(drInvLoc["wtavgcost"]);
                        dblOrigOnHand = CvtFunc.ToDouble(drInvLoc["OnHand"]);
                        dblOrigWtAvgCost = CvtFunc.ToDouble(drInvLoc["WtAvgCost"]);
                    }
                    else
                    {
                        drInvLoc.Close();
                        ImInvLocDataModel imLoc = new ImInvLocDataModel();
                        imLoc.CompanyID = intCompanyID;
                        imLoc.ItemID = intItemFInterID;
                        imLoc.StockUnitID = CvtFunc.ToInt(dbBaseCon.GetValue("select StockUnitID from iminvmaster where FInterID='" + intItemFInterID + "'"));
                        new ImInvLocBLL().Add(imLoc);
                    }
                }
                dblReturn = dblReturn * dblRate;
                double dblInTotalCost = dblQty * dblCost;

                double dblInQty = dblQty * dblRate;


                double dblTotalQty = dblOrigOnHand + dblInQty;
                double dblTotalCost = dblOrigTotalCost + dblInTotalCost;
                double dblNewWtAvgCost = 0;
                if (dblTotalQty == 0)
                {
                    dblNewWtAvgCost = dblOrigWtAvgCost;
                }
                else
                {
                    dblNewWtAvgCost = dblTotalCost / dblTotalQty;
                }

                strSQL = @"update iminvloc set OnHand=OnHand+@InQtyInStockUnit, wtAvgCost=@wtAvgCost,PurchasePrice=@PurchasePrice from iminvloc 
                            where companyid=@CompanyID and ItemID=@ItemID";
                dbInvCon.ExecuteSql(strSQL, new SqlParameter[]{new SqlParameter ( "@InQtyInStockUnit",dblInQty.ToString ())
                                                                         , new SqlParameter ( "@wtAvgCost",dblNewWtAvgCost.ToString ())
                                                                         , new SqlParameter ( "@PurchasePrice",dblCost.ToString ())
                                                                         , new SqlParameter ( "@CompanyID",intCompanyID.ToString ())
                                                                         , new SqlParameter ( "@ItemID", intItemFInterID.ToString ())});


                return true;
            }
        }

        internal static double GetOnOrder(int intCompanyID, int intItemFinterID, int intFromUnitID)
        {
            
            using (DBAccess dbOrder = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPSEOrder))
            )
            {
                return ImInvLocDAL.GetOnOrder_Req(dbOrder, intCompanyID, intItemFinterID, intFromUnitID)
                    + ImInvLocDAL.GetOnOrder_Order(dbOrder, intCompanyID, intItemFinterID, intFromUnitID)
                    + ImInvLocDAL.GetOnOrder_Recv(dbOrder, intCompanyID, intItemFinterID, intFromUnitID)
                    + ImInvLocDAL.GetOnOrder_Others(dbOrder, intCompanyID, intItemFinterID, intFromUnitID)
                    ;
            }

            
        }

        internal static double GetComm(int intCompanyID, int intItemFinterID, int intFromUnitID)
        {
            using (DBAccess dbOrder = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPSEOrder))
            )
            {
                return ImInvLocDAL.GetComm_Req(dbOrder, intCompanyID, intItemFinterID, intFromUnitID)
                    + ImInvLocDAL.GetComm_Others(dbOrder, intCompanyID, intItemFinterID, intFromUnitID)
                    ;
            }
        }
    }
}


